package methods;

import java.lang.reflect.Method;

class Person {

    private String name;
    private String gender;
    private String birthday;

    public Person() {
    }

    public Person(String name, String gender, String birthday) {
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    private void secrete(String content) {
        System.out.println("Show secrete is : " + content);
    }

    protected void sayHello(String msg) {
        System.out.println("hello," + msg);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday='" + birthday + '\'' +
                '}';
    }


}

public class ReflectMethodsDemo {

    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("methods.Person");
        Method[] methods = clazz.getMethods();
        System.out.println("---------the result for getMethods------------");
        for (Method m : methods) {
            System.out.println(m.toGenericString());
        }

        methods = clazz.getDeclaredMethods();
        System.out.println("---------the result for getDeclaredMethods------------");
        for (Method m : methods) {
            System.out.println(m.toGenericString());
        }

        Method m = clazz.getMethod("equals", Object.class);
        System.out.println("--------the result for getMethod----------");
        System.out.println(m.toGenericString());

        //m =  clazz.getMethod("secrete",String.class); //错误，不能获取私有方法。
        //System.out.println(m.toGenericString());
        System.out.println("--------the result for getDeclaredMethod----------");

        //m = clazz.getDeclaredMethod("equals",Object.class); //错误，equals不是自身声明的方法。
        //System.out.println(m.toGenericString());

        m = clazz.getDeclaredMethod("secrete", String.class);
        System.out.println(m.toGenericString());

        m.setAccessible(true); //私有方法执行前必须设置可访问。
        Person p = new Person();
        m.invoke(p, "this is a dream!");
    }
}
